package vashion.azeroth.back.controller.item;

import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import vashion.azeroth.back.controller.base.BaseController;
import vashion.azeroth.common.date.DateUtil;
import vashion.azeroth.common.util.ItemImageUtils;
import vashion.azeroth.contant.ItemErrorEnum;
import vashion.azeroth.core.oss.OSSImageProcessUtil;
import vashion.azeroth.core.result.ApiResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * 图片上传服务接口
 *
 * @param <T>
 */
@Controller
@RequestMapping("/back/image")
public class BackImageController<T> extends BaseController {

    private final static Logger log = LoggerFactory.getLogger(BackImageController.class);

    @RequestMapping("uploadItemFile.htm")
    @ResponseBody
    public ApiResult<String> uploadItemFile(ModelMap modelMap,
                                            HttpServletResponse response,
                                            HttpServletRequest request,
                                            @RequestParam("file") MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            //userId写进去哦
            //FIXME 根据userId 和 日期 生成相对路径，这样好找图片
            String imagePath = OSSImageProcessUtil.uploadItemFile(inputStream,
                    file.getContentType(), file.getOriginalFilename(), OSSImageProcessUtil.imagePath + "/" + DateUtil.ymdTimeFormat(new Date()));
            return ApiResult.newSuccessResult(imagePath);
        } catch (Exception e) {
            log.error("ImageController.uploadItemFile error", e);
            return ApiResult.newErrorResult(ItemErrorEnum.SYSTEM_ERROR.getErrorCode(), ItemErrorEnum.SYSTEM_ERROR.getErrorMessage());
        }
    }


    @RequestMapping("deleteFile.htm")
    @ResponseBody
    public ApiResult<Boolean> deleteFile(@RequestParam(value = "fileList") String fileList) {

        try {
            for (String imgUrl : fileList.split(",")) {
                String filePath = ItemImageUtils.removeImgCdnPrefix(imgUrl);
                OSSImageProcessUtil.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("ImageController.deleteFile error", e);
            return ApiResult.newErrorResult(ItemErrorEnum.SYSTEM_ERROR.getErrorCode(), ItemErrorEnum.SYSTEM_ERROR.getErrorMessage());
        }
        return ApiResult.newSuccessResult(true);
    }

    @RequestMapping("getFileList.htm")
    @ResponseBody
    public ApiResult<List<String>> getFileList(@RequestParam(value = "uploadTime", required = false) Long uploadTime) {

        String prefix = "";
        uploadTime = null == uploadTime ? DateUtil.getCurrentDateBegin().getTime() : uploadTime;
        if (null != uploadTime) {
            prefix = OSSImageProcessUtil.imagePath + "/" + DateUtil.ymdTimeFormat(new Date(uploadTime));
        }
        List<String> filePathList = OSSImageProcessUtil.getFileList(prefix);
        List<String> resultList = Lists.newArrayList();
        for (String path : filePathList) {
            resultList.add(ItemImageUtils.getImgFullPath(path));
        }
        return ApiResult.newSuccessResult(resultList);
    }

}
